<?php
declare (strict_types=1);

namespace app\admin\controller;

use think\facade\View;
use think\response\Json;

class AdPosition extends AdminBase
{
    public function lists(): string
    {
        return View::fetch();
    }

    public function list(): Json
    {
        $page               = $this->request->param('page', 1, 'intval');
        $limit              = $this->request->param('limit', 10, 'intval');
        $name               = $this->request->param('name', '', 'trim');
        $status             = $this->request->param('status', 0, 'intval');
        $adPositionBusiness = new \app\common\business\AdPosition($this->websiteCode);

        $where = [];
        if ($name) {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }
        if ($status) {
            $where[] = ['status', '=', $status];
        }

        $adPositionInfo = $adPositionBusiness->getAdPositionList($page, $limit, $where);
        return showList(config(self::_CONFIG_STATUS_SUCCESS_CODE), 'success', $adPositionInfo['rows'], $adPositionInfo['count']);
    }

    public function add(): string
    {
        return View::fetch();
    }

    public function doAdd(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $name   = $this->request->param('name', '', 'trim');
        $status = $this->request->param('status', 0, 'intval');

        $data     = ['name' => $name, 'status' => $status == self::_IS_ENABLE_STATUS ? self::_IS_ENABLE_STATUS : self::_IS_DISABLE_STATUS];
        $validate = (new \app\admin\validate\AdPosition())->scene('add');
        if (!$validate->check($data)) {
            return show(config("status.error"), $validate->getError());
        }

        $adPositionBusiness = new \app\common\business\AdPosition($this->websiteCode);
        $res                = $adPositionBusiness->addAdPosition($data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function edit(): string
    {
        $id = $this->request->param('id', 0, 'intval');
        if (!$id) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        $adPositionBusiness = new \app\common\business\AdPosition($this->websiteCode);
        $adPosition         = $adPositionBusiness->findAdPositionById($id);
        if (empty($adPosition)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }
        View::assign(["adPosition" => $adPosition]);

        return View::fetch();
    }

    public function doEdit(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $id     = $this->request->param('id', 0, 'intval');
        $name   = $this->request->param('name', '', 'trim');
        $status = $this->request->param('status', 0, 'intval');

        $data     = ['name' => $name, 'status' => $status == self::_IS_ENABLE_STATUS ? self::_IS_ENABLE_STATUS : self::_IS_DISABLE_STATUS];
        $validate = (new \app\admin\validate\AdPosition())->scene('edit');
        if (!$validate->check($data)) {
            return show(config("status.error"), $validate->getError());
        }

        $adPositionBusiness = new \app\common\business\AdPosition($this->websiteCode);
        $res                = $adPositionBusiness->updateAdPosition($id, $data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }


    public function doDelete(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $id = $this->request->param('id', 0, 'intval');
        if ($id == 0) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $adPositionBusiness = new \app\common\business\AdPosition($this->websiteCode);
        $res                = $adPositionBusiness->deleteAdPosition($id);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }
}
